<template>
  <div class="register-container">
    <div class="register-left">
      <h1 class="system-title">交通违法识别管理系统</h1>
      <p class="system-desc">智能识别 · 高效处理 · 实时预警</p>
    </div>
    <div class="register-right">
      <el-card class="register-box">
        <h2 class="register-title">用户注册</h2>
        <el-form :model="registerForm" :rules="rules" ref="registerFormRef" label-width="80px">
          <el-form-item label="用户名" prop="username">
            <el-input v-model="registerForm.username" placeholder="请输入用户名" />
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input v-model="registerForm.password" type="password" placeholder="请输入密码" show-password />
          </el-form-item>
          <el-form-item label="确认密码" prop="confirmPassword">
            <el-input v-model="registerForm.confirmPassword" type="password" placeholder="请再次输入密码" show-password />
          </el-form-item>
          <el-form-item label="性别" prop="gender">
            <el-radio-group v-model="registerForm.gender">
              <el-radio label="male">男</el-radio>
              <el-radio label="female">女</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="身份证号" prop="idNumber">
            <el-input v-model="registerForm.idNumber" placeholder="请输入身份证号" />
          </el-form-item>
          <el-form-item label="手机号" prop="phone">
            <el-input v-model="registerForm.phone" placeholder="请输入手机号" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="handleRegister" class="register-btn">注册</el-button>
          </el-form-item>
          <div class="register-options">
            <router-link to="/login" class="login-link">已有账号？立即登录</router-link>
          </div>
        </el-form>
      </el-card>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { register } from '@/api/auth'

const router = useRouter()

const registerForm = reactive({
  username: '',
  password: '',
  confirmPassword: '',
  phone: ''
})

const validatePass = (rule, value, callback) => {
  if (value !== registerForm.password) {
    callback(new Error('两次输入密码不一致!'))
  } else {
    callback()
  }
}

const rules = reactive({
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' }
  ],
  confirmPassword: [
    { required: true, message: '请再次输入密码', trigger: 'blur' },
    { validator: validatePass, trigger: 'blur' }
  ],
  gender: [
    { required: true, message: '请选择性别', trigger: 'change' }
  ],
  idNumber: [
    { required: true, message: '请输入身份证号', trigger: 'blur' }
  ],
  phone: [
    { required: true, message: '请输入手机号', trigger: 'blur' }
  ]
})

const registerFormRef = ref()

const handleRegister = async () => {
  try {
    await registerFormRef.value.validate()
    await register(registerForm)
    console.log()
    router.push('/login')
  } catch (error) {
    console.error('注册失败:', error)
  }
}
</script>

<style scoped>
.register-container {
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 100vh;
  background: url('@/assets/login-bg.jpg') no-repeat center center;
  background-size: cover;
  padding: 0 10%;
}

.register-left {
  color: white;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.3);
}

.system-title {
  font-size: 2.5em;
  margin-bottom: 20px;
}

.system-desc {
  font-size: 1.2em;
  opacity: 0.9;
}

.register-right {
  width: 500px;
}

.register-box {
  padding: 30px;
  border-radius: 8px;
  box-shadow: 0 0 20px rgba(0, 0, 0, 0.1);
  background: rgba(255, 255, 255, 0.95);
}

.register-title {
  text-align: center;
  margin-bottom: 30px;
  color: #333;
  font-size: 1.5em;
}

.register-btn {
  width: 100%;
  margin-top: 10px;
}

.register-options {
  text-align: center;
  margin-top: 15px;
}

.login-link {
  color: #409EFF;
  text-decoration: none;
}

.login-link:hover {
  color: #66b1ff;
}
</style>